package com.hy.mydesktop.client.component.factory;

import java.util.LinkedList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.Layout;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Window;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hy.mydesktop.client.component.meta.AbstractComponetModelEnum;
import com.hy.mydesktop.client.component.meta.AccordionPanelModelEnum;
import com.hy.mydesktop.client.component.meta.TextFieldModelEnum;
import com.hy.mydesktop.client.component.meta.WindowModelEnum;
import com.hy.mydesktop.client.component.type.ComponentTypeEnum;
import com.hy.mydesktop.client.rpc.ServiceInvoker;
import com.hy.mydesktop.client.rpc.ServiceInvokerAsync;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.ComponentMetaDataConstants;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceParameter;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceResult;
import com.hy.mydesktop.shared.rpc.util.ComponentTypeEnumConverter;
import com.hy.mydesktop.shared.util.log.MyLoggerUtil;

/**
 * 
 * <ul>
 * <li>开发作者：花宏宇</li>
 * <li>设计日期：2010-11-10；时间：下午02:18:13</li>
 * <li>类型名称：AbstractComponetFactory</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class AbstractLayoutContainerComponetFactory {
	
	
	public static final String className = "com.hy.mydesktop.client.component.factory.AbstractLayoutContainerComponetFactory";

	/**
	 * 
	 * <ul>
	 * <li>方法含义：创建Window控件</li>
	 * <li>方法作者：汤莉</li>
	 * <li>编写日期：2010-8-29；时间：下午下午03:05:09</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param gxtComponentMetaNodeModel
	 * @return
	 */
	public static <T>T configureAbstractLayoutContainerComponet(LayoutContainer component,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		
		return (T)configureComponent(component,gxtComponentMetaNodeModel);
	}
	
	public static GxtComponentMetaNodeModel removeBaseGxtComponentId(GxtComponentMetaNodeModel gxtComponentMetaNodeModel){

		//设置基础控件的Id
		if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.COMPONENT_ID)!=null){
			gxtComponentMetaNodeModel.set(ComponentMetaDataConstants.COMPONENT_ID,null);
			
			gxtComponentMetaNodeModel.set(ComponentMetaDataConstants.ID,null);
		
		}
		if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.ID)!=null){
			//设置基础控件的Id
			gxtComponentMetaNodeModel.set(ComponentMetaDataConstants.ID,null);
			
			gxtComponentMetaNodeModel.set(ComponentMetaDataConstants.COMPONENT_ID,null);
			
		}
		
		return gxtComponentMetaNodeModel;
	}
	
	
	private static Component configureBaseComponent(LayoutContainer component,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){


		
		
		
		if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.LAYOUT_INDEX)!=null){
			
			if (component instanceof LayoutContainer) {
				Integer componentTypeEnumIndex = (Integer)gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.LAYOUT_INDEX);					
				if (componentTypeEnumIndex>0) {
					ComponentTypeEnum componentTypeEnum = 	ComponentTypeEnumConverter.getComponentTypeEnum(componentTypeEnumIndex);
					component =setLayoutOfLayoutContainer((LayoutContainer)component,componentTypeEnum, gxtComponentMetaNodeModel);
					
				}
					
			}		
		}
		
		
		if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.LAYOUT)!=null){
			//设置布局方式
			Object object = gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.LAYOUT);
			ComponentTypeEnum componentTypeEnum = null;
			if (object instanceof Integer) {
				Integer componentTypeEnumIndex = (Integer)gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.LAYOUT);		
				componentTypeEnum = 	ComponentTypeEnumConverter.getComponentTypeEnum(componentTypeEnumIndex);
			}else {
				componentTypeEnum = (ComponentTypeEnum)object;
			}
	
			component =setLayoutOfLayoutContainer((LayoutContainer)component,componentTypeEnum, gxtComponentMetaNodeModel);
			
		
		}
		
		//自动调节宽度
		if (gxtComponentMetaNodeModel.get(AccordionPanelModelEnum.AUTOWIDTH.name()
				.toLowerCase()) != null) {
			Object autoWidth =  gxtComponentMetaNodeModel
					.get(AccordionPanelModelEnum.AUTOWIDTH.name().toLowerCase());
			if (autoWidth instanceof Boolean) {				
				component.setAutoWidth((Boolean)autoWidth);
			} else {

				if ("true".equals(autoWidth.toString())) {
					
					component.setAutoWidth(true);
				} else {

					component.setAutoWidth(false);
				}
			}

		} 
		//自动调节高度
		if (gxtComponentMetaNodeModel.get(AccordionPanelModelEnum.AUTOHEIGHT.name()
				.toLowerCase()) != null) {
			Object autoHeight =  gxtComponentMetaNodeModel
					.get(AccordionPanelModelEnum.AUTOHEIGHT.name().toLowerCase());
			if (autoHeight instanceof Boolean) {				
				component.setAutoHeight((Boolean)autoHeight);
			} else {

				if ("true".equals(autoHeight.toString())) {
					
					component.setAutoWidth(true);
				} else {

					component.setAutoWidth(false);
				}
			}

		} 
		
		
		return component;
	}
	private static LayoutContainer setLayoutOfLayoutContainer(LayoutContainer layoutContainer ,ComponentTypeEnum componentTypeEnum,GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {
		//通过索引，设置布局方式
		
			Layout layout=null;
				
				switch (componentTypeEnum) {
	            case FITLAYOUT_MODEL:
	            		layout=FitLayoutFactory.createFitLayout(gxtComponentMetaNodeModel);
	            		layoutContainer.setLayout(layout);
	                break;
	            case ACCORDIONLAYOUT_MODEL:
	                	layout=AccordionLayoutFactory.createAccordionLayout(gxtComponentMetaNodeModel);
	                	layoutContainer.setLayout(layout);
	                break;
	            case ROWLAYOUT_MODEL:
	                	layout=RowLayoutFactory.createRowLayout(gxtComponentMetaNodeModel);
	                	layoutContainer.setLayout(layout);
	                break;
	            case VBOXLAYOUT_MODEL:
	                	layout=VboxLayoutFactory.createVboxLayout(gxtComponentMetaNodeModel);
	                	layoutContainer.setLayout(layout);
	                break;
	            case COLUMNLAYOUT_MODEL:
	                	layout=ColumnLayoutFactory.createColumnLayout(gxtComponentMetaNodeModel);
	                	layoutContainer.setLayout(layout);
	                break;
	            case FLOWLAYOUT_MODEL:
	                	layout=FlowLayoutFactory.createFlowLayout(gxtComponentMetaNodeModel);
	                	layoutContainer.setLayout(layout);
	                break;
	            case TABLELAYOUT_MODEL:
	                	layout=TableLayoutFactory.createTableLayout(gxtComponentMetaNodeModel);
	                	layoutContainer.setLayout(layout);
	                break;
	            case FORMLAYOUT_MODEL:
	                	layout=FormLayoutFactory.createFormLayout(gxtComponentMetaNodeModel);
	                	
	                	layoutContainer.setLayout(layout);
	                	
	                break;
	            default:
	                break;
	        }
			//}
							
		return layoutContainer;
		
	}

	/**
	 * 
	 * <ul>
	 * <li>方法含义：对窗口的属性进行配置</li>
	 * <li>方法作者：汤莉</li>
	 * <li>编写日期：2010-8-29；时间：下午下午03:56:36</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param gxtComponentMetaNodeModel
	 * @return
	 */
	private static Component configureComponent(LayoutContainer component,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		
		
		configureBaseComponent(component, gxtComponentMetaNodeModel);
		
		
		return component;
	}
	
	
	
	
	
	


	
	
}
